/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.bean.ExamBean;
import model.dao.ExamDAO;
import model.dto.Exam;
import model.util.SharingUtil;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author Aptech
 */
public class ExamAction extends BaseAction {

    ExamDAO daoExam = new ExamDAO();

    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward("add");
    }

    public ActionForward edit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int examId = Integer.parseInt(request.getParameter("id") == null ? "0" : request.getParameter("id"));
        Exam e = daoExam.getExam(examId);
        if (e.getId() <= 0) {
            SharingUtil.showMessage(request, new ActionMessage("edit.failure"));
        }
        request.setAttribute("exam", e);
        return mapping.findForward("edit");
    }

    public ActionForward create(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Exam e = new Exam();
        ExamBean bnExam = (ExamBean) form;
        e.setName(bnExam.getName());
        e.setTime(bnExam.getTime());
        e.setStartTime(bnExam.getStartTime());
        e.setEndTime(bnExam.getEndTime());
        e.setState(bnExam.getState());
        e.setAuthor(SharingUtil.getUser(request.getSession()));
        e.setCls(SharingUtil.getCurrentClass(request.getSession()));
        e.setCreatedDate(new Date());
        if (daoExam.create(e) > 0) {
            SharingUtil.showMessage(request, new ActionMessage(MSG_CREATE_SUCCESS));
            return mapping.findForward("edit");
        } else {
            SharingUtil.showMessage(request, new ActionMessage(MSG_CREATE_FAILURE));
            return mapping.findForward("add");
        }

    }

    public ActionForward update(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Exam e = new Exam();
        ExamBean bnExam = (ExamBean) form;
        e.setId(bnExam.getId());
        e.setName(bnExam.getName());
        e.setTime(bnExam.getTime());
        e.setStartTime(bnExam.getStartTime());
        e.setEndTime(bnExam.getEndTime());
        e.setState(bnExam.getState());
        e.setAuthor(SharingUtil.getUser(request.getSession()));
        e.setCls(SharingUtil.getCurrentClass(request.getSession()));
        e.setCreatedDate(new Date());
        if (daoExam.update(e) > 0) {
            SharingUtil.showMessage(request, new ActionMessage(MSG_CREATE_SUCCESS));
        } else {
            SharingUtil.showMessage(request, new ActionMessage(MSG_CREATE_FAILURE));
        }
        return mapping.findForward("edit");
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int examId = Integer.parseInt(request.getParameter("id") == null ? "0" : request.getParameter("id"));
        if (daoExam.delete(examId) > 0) {
            SharingUtil.showMessage(request, new ActionMessage(MSG_DELETE_SUCCESS));
        } else {
            SharingUtil.showMessage(request, new ActionMessage(MSG_DELETE_FAILURE));
        }
        return mapping.findForward("list");
    }

    public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward(SUCCESS);
    }
}
